/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lmx.repoDomain.repository;

import javax.annotation.PostConstruct;
import javax.persistence.Query;

import com.lmx.common.constants.ICommonConstants;
import com.lmx.common.context.ActionContextHolder;
import com.lmx.persistence.service.DataProcessService;
import com.lmx.persistence.service.HuaweiService;
import com.lmx.persistence.service.MessageService;
import com.lmx.repoDomain.tmodel.*;
import io.katharsis.repository.annotations.JsonApiSave;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import io.katharsis.repository.annotations.JsonApiResourceRepository;

import java.util.ArrayList;
import java.util.Date;

@JsonApiResourceRepository(UserTask.class)
@Component
public class UserTaskRepository extends BaseRepository<UserTask>{
    private Log logger = LogFactory.getLog(this.getClass());
    @Autowired
    private DataProcessService dataProcessService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private HuaweiService huaweiService;

    @PostConstruct
    public void init() {
        dslJpaService.setEntityClass(UserTask.class);
    }

    @Override
    @JsonApiSave
    public UserTask save(UserTask userTask) {
        logger.debug("-------------------------------userTask   save-----------------------------------------");
        if (userTask.getId() == null) {
            logger.debug("userTask create");
            //任务申请提醒
            if(userTask.getStatus() != null && "taskApply".equals(userTask.getStatus().getTypecode())){
                if(!dataProcessService.checkUserIsVerify(userTask.getUser().getId())){
                    logger.debug("客服未认证 任务不能申请");
                    userTask.setErrcode(ICommonConstants.INTERFACEERRO.OTHERFAIL.ordinal());
                    return userTask;
                }
                logger.debug("userTask 任务申请");
                userTask = (UserTask)dslJpaService.create(userTask);
//                dataProcessService.addUserTask(userTask);
                messageService.taskRemind(userTask, "taskApply");
            }else {
                userTask = (UserTask)dslJpaService.create(userTask);
//                dataProcessService.addUserTask(userTask);
            }
            //添加关联表数据
            dataProcessService.addUserTask(userTask);
            logger.debug("userTask 创建");

        } else {
            logger.debug("userTask update");
            String action = ActionContextHolder.getContext().getAction();
            if(StringUtils.isNotBlank(action) && "sign".equals(action)){
                logger.debug("userTask 任务签到");
                userTask = dataProcessService.updateSignStatus(userTask);
            }else if(StringUtils.isNotBlank(action) && "approved".equals(action)){
                if(dataProcessService.isNeed(userTask.getTask().getId())){
                    logger.debug("userTask 任务审核通过");
                    dslJpaService.update(userTask);
                    huaweiService.addOperator(userTask.getId());//添加操作员
//                    userTask = (UserTask)dslJpaService.update(userTask);
                    userTask = (UserTask)dslJpaService.findById(userTask.getId());
                    dataProcessService.updateCsNum(userTask);//更新taskExtend (已招客服数量)
                    messageService.taskRemind(userTask, "taskApplySuccNotLocateSeat");
                }else {
                    logger.debug("userTask 客服数量验证异常");
                    userTask.setErrcode(ICommonConstants.INTERFACEERRO.OTHERFAIL.ordinal());
                }

            }else if(StringUtils.isNotBlank(action) && "notApproved".equals(action)){
                logger.debug("userTask 任务审核不通过");
                messageService.taskRemind(userTask, "taskApplyFail");
                userTask = (UserTask)dslJpaService.update(userTask);
            }else if(StringUtils.isNotBlank(action) && "agreeInvite".equals(action)){

                if(!dataProcessService.checkUserIsVerify(userTask.getUser().getId())){
                    logger.debug("客服未认证 任务不能申请");
                    userTask.setErrcode(ICommonConstants.INTERFACEERRO.OTHERFAIL.ordinal());
                    return userTask;
                }
                //同意邀请 提醒企业
                if(dataProcessService.isNeed(userTask.getTask().getId())){
                    logger.debug("userTask 客服同意邀请");
                    dslJpaService.update(userTask);
                    huaweiService.addOperator(userTask.getId());//添加操作员
//                    userTask = (UserTask)dslJpaService.update(userTask);
                    userTask = (UserTask)dslJpaService.findById(userTask.getId());
                    dataProcessService.updateCsNum(userTask);//更新taskExtend (已招客服数量)
                    messageService.taskRemind(userTask, "taskApplySuccNotLocateSeat");
                }else {
                    logger.debug("userTask 客服数量验证异常");
                    userTask.setErrcode(ICommonConstants.INTERFACEERRO.OTHERFAIL.ordinal());
                }
            }else if(StringUtils.isNotBlank(action) && "refusedInvite".equals(action)){
                logger.debug("userTask 客服不同意邀请");
                messageService.taskRemind(userTask, "taskApplyRefuseInvitation");
                //不同意邀请  提醒企业
                userTask = (UserTask)dslJpaService.update(userTask);
            }else {
                logger.debug("userTask 更新");
                userTask = (UserTask)dslJpaService.update(userTask);
            }

        }
        logger.debug("return userTask");
        return userTask;
    }


}
